var express = require('express');
const createHttpError = require('http-errors');
var router = express.Router();
var axios = require('axios')
var { UserModel, Fmshoucang, pinglun } = require('../untils/model')
var { createToken, checkToken } = require("../untils/token")


router.get("/gethome", (req, res) => {
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/pagecontent/list", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})


router.get("/getnav", (req, res) => {
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/new/navigate/list?", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})

router.get("/gettuijian", (req, res) => {
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/rank/list", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})

router.get("/getjiemu", (req, res) => {
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/audios/list", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})

router.get("/getxiangqing", (req, res) => {
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/albumdetail/get", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})


router.get("/getsousuo", (req, res) => { //搜索
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/resource/searchall", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})





router.get("/getkey", (req, res) => { //热门搜索
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/api/v4/resource/hotwords", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})


router.get("/getword", (req, res) => { //推荐搜索
    // var headers = req.headers;
    axios.get("https://api.kaolafm.com/resource/searchwords", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})

router.get("/getvideo", (req, res) => {
    // var headers = req.headers;
    axios.get("http://m.kaolafm.com/webapi/audios/list", {
        params: req.query,
    }).then(result => {
        res.json({
            code: 200,
            msg: "获取数据成功",
            ...result.data
        })
    }).catch(err => {
        res.json({
            code: 500, // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })
})


// 获取用户信息
router.get("/getuserinfo", (req, res) => {

    // 判断身份的有效性 
    checkToken(req, res, ({ username }) => {
        console.log(username)
        UserModel.findOne({
            usernames: username
        }).then(result => {
            res.json({
                code: 200,
                msg: "获取用户信息数据成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500, // 500 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})


router.get("/Fmshoucang", (req, res) => { //收藏FM
    // req.query
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        if (result) {
            Fmshoucang.insertMany({ id: query.id, albumid: query.albumid, username: query.username }).then(data => {
                res.json({
                    code: 200,
                    msg: "添加成功",
                    type: 1,
                    data
                })
            })
        } else {
            res.json({
                code: 1000,
                msg: "用户名不存在",
                type: 0,
            })
        }
    })
})

router.get("/FMaddpl", (req, res) => { //添加评论
    // req.query
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        console.log("已找到用户")
        if (result) {
            pinglun.insertMany({ id: query.id, albumid: query.albumid, username: query.username, pl: query.pl, time: query.time }).then(data => {
                res.json({
                    code: 200,
                    msg: "添加成功",
                    type: 1,
                    data
                })
            })
        } else {
            res.json({
                code: 1000,
                msg: "用户名不存在",
                type: 0,
            })
        }
    })
})



router.get("/FMfindallpl", (req, res) => { //查询歌单所有评论
    // req.query
    var query = req.query
    console.log(query)
        // UserModel.findOne({ usernames: query.username }).then(result => {
    console.log("已找到用户")

    pinglun.find({ id: query.id, albumid: query.albumid, }).then(data => {
        res.json({
            code: 200,
            msg: "查询成功",
            type: 1,
            data
        })
    })

    // })
})

router.get("/FMuserpl", (req, res) => { //查询用户所有评论
    // req.query
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        console.log("已找到用户")
        if (result) {
            pinglun.find({ username: query.username }).then(data => {
                res.json({
                    code: 200,
                    msg: "查询成功",
                    type: 1,
                    data
                })
            })
        } else {
            res.json({
                code: 1000,
                msg: "用户名不存在",
                type: 0,
            })
        }
    })
})

router.get("/delpl", (req, res) => { //查询用户所有评论
    // req.query
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        console.log("已找到用户")
        if (result) {
            pinglun.remove({ _id: query._id }).then(data => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    type: 1,
                    data
                })
            })
        } else {
            res.json({
                code: 1000,
                msg: "用户名不存在",
                type: 0,
            })
        }
    })
})




router.get("/Fmfindall", (req, res) => { //查询用户所有收藏
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        if (result) {
            Fmshoucang.find({ username: query.username }).then(data => {
                if (data) {
                    res.json({
                        code: 200,
                        msg: "查询成功",
                        data,
                        type: 1,
                    })
                } else {
                    res.json({
                        code: 300,
                        msg: "null",
                        type: 0,
                    })
                }
            })
        }
    })
})



router.get("/Fmfindone", (req, res) => { //查询歌单是否收藏
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        if (result) {
            Fmshoucang.findOne({ id: query.id, albumid: query.albumid,username: query.username }).then(data => {
                if (data) {
                    res.json({
                        code: 200,
                        msg: "该歌单已收藏",
                        type: 1,
                    })
                } else {
                    res.json({
                        code: 300,
                        msg: "该歌单未收藏",
                        type: 0,
                    })
                }
            })
        } else {
            res.json({
                code: 2000,
                msg: "用户未登录",
                type: 0,
            })
        }
    })
})


router.get("/Fmdelsc", (req, res) => { //取消收藏
    var query = req.query
    console.log(query)
    UserModel.findOne({ usernames: query.username }).then(result => {
        if (result) {
            Fmshoucang.findOne({ id: query.id, albumid: query.albumid }).then(data => {
                if (data) {
                    Fmshoucang.remove({ id: query.id, albumid: query.albumid }).then(gd => {
                        if (gd) {
                            res.json({
                                code: 200,
                                msg: '取消收藏成功',
                                type: 1,
                            })
                        }
                    })
                } else {
                    res.json({
                        code: 300,
                        msg: "该歌单未收藏",
                        type: 0,
                    })
                }
            })
        }
    })
})



// 上传头像  
// multer 
var multer = require("multer");
const { json } = require('express');
var storage = multer.diskStorage({
    destination: function(req, file, cb) { // 头像存放的目录 
        cb(null, './public/avatar') // . 文件路径
    },
    filename: function(req, file, cb) { // 新的文件名 
        cb(null, Date.now() + "WH2008" + file.originalname); //  originalname 原文件名  1.jpg    145123213123WH20081.jpg
    }
})

var upload = multer({ storage: storage }).any(); // 可以上传任何格式的图片

router.post('/uploadavatar', upload, (req, res) => {
    checkToken(req, res, ({ username }) => {
        console.log(req.files)
        if (req.files) {
            var path = req.files[0].path;
            UserModel.updateMany({
                usernames: username,
            }, {
                $set: {
                    avatar: path
                }
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "头像上传成功",
                    avatar: path,
                    result
                })
            }).catch(err => {
                res.json({
                    code: 500, // 500 统一失败  
                    msg: "服务器异常- 未知错误",
                    type: 0,
                })
            })
        }
    })
})



module.exports = router;